<template>
  <div class="container">
    <el-row style="margin-bottom: 10px;">
      <el-button type="primary" icon="el-icon-plus" @click="addUser">添加用户</el-button>
    </el-row>
    <el-table :header-cell-style="{ backgroundColor: '#f2f3f5', borderColor: '#C0C4CC', color: '#409EFF' }" :data="tableData" border style="width: 100%">
      <el-table-column align="center" prop="uname" label="账号" min-width="105px" />
      <el-table-column align="center" prop="uygxm" label="姓名" min-width="105px" />
      <el-table-column align="center" prop="uygbh" label="工号" min-width="105px" />
      <el-table-column align="center" prop="power" label="权限" min-width="105px" />
    </el-table>
    <el-pagination
      :current-page.sync="currentPage"
      :page-sizes="[10, 20, 50]"
      :page-size.sync="pageSize"
      style="float: right;margin-top: 10px;"
      layout="total, sizes, prev, pager, next"
      :total="totalCounts"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
    <el-dialog width="400px" class="add-user" title="添加用户" :close-on-click-modal="false" :visible.sync="dialogFormVisible" @closed="handleFormDialogClosed">
      <el-form ref="form" :model="form" :rules="rules" label-width="70px">
        <el-form-item label="账号" prop="uname">
          <el-input v-model.trim="form.uname" autocomplete="off" />
        </el-form-item>
        <el-form-item label="密码" prop="upassword">
          <el-input v-model.trim="form.upassword" autocomplete="off" />
        </el-form-item>
        <el-form-item label="姓名" prop="uygxm">
          <el-input v-model.trim="form.uygxm" autocomplete="off" />
        </el-form-item>
        <el-form-item label="工号" prop="uygbh">
          <el-input v-model.trim="form.uygbh" autocomplete="off" />
        </el-form-item>
        <el-form-item label="权限" prop="power">
          <el-select v-model="form.power" placeholder="请选择">
            <el-option label="管理员" value="管理员" />
            <el-option label="操作员" value="操作员" />
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="addUserConfirm">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getUsers, addUser } from '@/api/delivery'
export default {
  name: 'User',
  data() {
    return {
      tableData: [],
      originTableData: [],
      currentPage: 1,
      pageSize: 10,
      totalCounts: 0,
      dialogFormVisible: false,
      form: {
        uname: '',
        upassword: '',
        uygxm: '',
        uygbh: '',
        power: ''
      },
      rules: {
        uname: [
          { required: true, message: '请输入账号', trigger: 'blur' }
        ],
        upassword: [
          { required: true, message: '请输入密码', trigger: 'blur' }
        ],
        uygxm: [
          { required: true, message: '请输入姓名', trigger: 'blur' }
        ],
        uygbh: [
          { required: true, message: '请输入工号', trigger: 'blur' }
        ],
        power: [
          { required: true, message: '请选择权限', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.getUserList()
  },
  methods: {
    getUserList() {
      getUsers().then(res => {
        this.originTableData = res
        this.totalCounts = this.originTableData.length
        this.currentPage = 1
        this.handleCurrentChange(1)
      })
    },
    handleSizeChange(size) {
      this.currentPage = 1
      this.handleCurrentChange(1)
    },
    handleCurrentChange(page) {
      this.tableData = this.originTableData.slice((page - 1) * this.pageSize, page * this.pageSize)
    },
    formatterTime(row, column, cellValue) {
      if (cellValue) {
        return new Date(cellValue).toLocaleString().replace(/\//g, '-')
      }
    },
    addUser() {
      this.dialogFormVisible = true
    },
    addUserConfirm() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          addUser(this.form).then(res => {
            console.log(res)
            this.dialogFormVisible = false
            this.$message({
              type: 'success',
              message: '添加成功!'
            })
            this.getUserList()
          })
        } else {
          return false
        }
      })
    },
    handleFormDialogClosed() {
      this.$refs.form.resetFields()
    }
  }
}
</script>

<style lang="scss" scoped>
.container {
  padding: 20px;
  padding-bottom: 50px;
}
.el-table {
  border-color: #C0C4CC;
  border-bottom: 1px solid #C0C4CC;
  ::v-deep {
    th>.cell {
      font-size: 20px;
    }
    td>.cell {
      font-size: 18px;
    }
  }
}
.el-pagination {
  ::v-deep {
    .el-pagination__total, i.el-icon  {
      font-size: 18px;
    }
    .el-input__inner {
      font-size: 16px;
    }
    .el-pager li {
      font-size: 18px;
    }
  }
}
.add-user {
  .el-input {
    width: 90%;
  }
  .el-select {
    width: 90%;
  }
}
</style>
